//
//  modifyInfoViewController.m
//  zhijiansuzhou
//
//  Created by 孔晓寅 on 16/3/31.
//  Copyright © 2016年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "modifyInfoViewController.h"

@interface modifyInfoViewController (){
    NSDictionary *input;
}

@end

@implementation modifyInfoViewController

- (instancetype)initWithInfo:(NSObject *)obj{
    self = [super init];
    if (self) {
        input = (NSDictionary*)obj;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCustomRightText:self action:@selector(loginclick:) text:@"保存"];
    [self initControl];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loginclick:(id)sender{
    [self.view endEditing:YES];
    [self loginRequest];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void) initControl {
    [self.view setBackgroundColor:[utils getBackGroupColorGray]];
    CGFloat bottom = [utils getViewBottom:self.navigationController.navigationBar]+OFFSET_TWN;
    
    _textEdit = [[UITextField alloc] init];
    UIView *textEditView;
    
    if ([[input objectForKey:@"flag"] isEqualToString:@"1"]) {//修改昵称
        self.title = @"修改昵称";
        textEditView = [self setEdit:@"icon_yanzhengma" withview:_textEdit withtext:[input objectForKey:@"title"] withplaceholder:@"请输入昵称" withY:bottom];
    }else if([[input objectForKey:@"flag"] isEqualToString:@"2"]){//修改用户名
        self.title = @"修改用户名";
        textEditView = [self setEdit:@"icon_yanzhengma" withview:_textEdit withtext:[input objectForKey:@"title"] withplaceholder:@"请输入用户名" withY:bottom];
    }else if([[input objectForKey:@"flag"] isEqualToString:@"3"]){//修改邮箱
        self.title = @"修改邮箱";
        textEditView = [self setEdit:@"icon_yanzhengma" withview:_textEdit withtext:[input objectForKey:@"title"] withplaceholder:@"请输入邮箱" withY:bottom];
    }
    
    _clear_button = [utils getButton:CGRectMake(textEditView.frame.size.width - _textEdit.frame.size.height/2 - 10 , 0, _textEdit.frame.size.height/2, _textEdit.frame.size.height) withName:@""];
    _clear_button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _clear_button.backgroundColor = [UIColor clearColor];
    [_clear_button setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_clear_button setBackgroundImage:nil forState:UIControlStateSelected];
    [_clear_button setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    
    [textEditView addSubview:_clear_button];
    [_clear_button addTarget:self action:@selector(clearclick:) forControlEvents: UIControlEventTouchUpInside];
    
    [self.view addSubview:textEditView];
    bottom = [utils getViewBottom:textEditView]+0.5 ;
    
}

-(IBAction)clearclick:(id)sender{
    
    _textEdit.text = @"";
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y with_w:(CGFloat)width{
    
    CGFloat left_x = 0;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(left_x, point_y, width-left_x*2, 44)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    //    UIImage *img = [UIImage imageNamed:img_url];
    //    CGFloat icon_img_h = view.frame.size.height - 28;
    //    CGFloat icon_img_w = img.size.width/img.size.height*icon_img_h;
    //    UIImageView *icon_img = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, icon_img_w, icon_img_h)];
    //    [icon_img setImage:img];
    
    [subview setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    subview.text = text;
    subview.textColor = [UIColor blackColor];
    subview.font = [UIFont systemFontOfSize:15];
    subview.placeholder = placeholder;
    [subview.layer setCornerRadius:5];
    [subview.layer setBorderWidth:0];
    [subview.layer setBorderColor:[[utils getBorderColorGray] CGColor]];
    [subview setBackgroundColor:[UIColor whiteColor]];
    subview.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 0)];
    subview.leftViewMode = UITextFieldViewModeAlways;
    subview.delegate = self;
    //    [subview addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    subview.returnKeyType = UIReturnKeyDone;
    
    [view addSubview:subview];
    //    [view addSubview:icon_img];
    
    return view;
    
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y{
    
    return [self setEdit:img_url withview:subview withtext:text withplaceholder:placeholder withY:point_y with_w:self.view.frame.size.width];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    CGRect frame = [textField superview].frame;
    //    CGFloat offset_y = self.myScrollView.contentOffset.y;
    
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset = (frame.origin.y+120)-(self.view.frame.size.height-216.0);//键盘高度216
    
    if (offset>0) {
        
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        
        [UIView setAnimationDuration:0.30f];//动画持续时间
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    //输入框编辑完成以后，当键盘即将消失时，将视图恢复到原始状态
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

-(void)loginRequest{
    
    InfoDataModel *personInfoModel = [utils getPersonInfo];
    
    if ([[input objectForKey:@"flag"] isEqualToString:@"1"]) {//修改昵称

            NSString *newName = _textEdit.text;
            if (newName && ![newName isEqualToString:@""]) {
                
         
                [HttpNetWorkUtil requestupdateNick:@{@"nick":newName} success:^(id json) {
             
                    
                    BaseModel *model = [[BaseModel alloc] init];
                    [model fillWithJsonDict:json];
                    if (![model.message isEqualToString:@""]) {
                        [OMGToast showWithText:model.message bottomOffset:50 duration:1.5];
                    }
                    
                    
                    personInfoModel.infomodel.m.nick = newName;
                    NSData *person = [RFJModel toDataWithModel:personInfoModel];
                    [utils setObject:person forkey:PERSONINFO];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                } fail:^{}];
                
            }else{
                [OMGToast showWithText:@"请输入昵称" duration:1.0];
            }

    }else if([[input objectForKey:@"flag"] isEqualToString:@"2"]){//修改用户名

            NSString *newUser = _textEdit.text;
            if (newUser && ![newUser isEqualToString:@""]) {
                
               
                [HttpNetWorkUtil requestupdateName:@{@"name":newUser} success:^(id json) {
              
                    personInfoModel.infomodel.m.name = newUser;
                    NSData *person = [RFJModel toDataWithModel:personInfoModel];
                    [utils setObject:person forkey:PERSONINFO];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                } fail:^{}];
                
            }else{
                [OMGToast showWithText:@"请输入用户名" duration:1.0];
            }
    }else if([[input objectForKey:@"flag"] isEqualToString:@"3"]){//修改邮箱

            NSString *newMail = _textEdit.text;
            if (newMail && ![newMail isEqualToString:@""]) {
                
                if ([utils isValidateEmail:newMail]) {
                    
             
                    [HttpNetWorkUtil requestupdateEmail:@{@"email":newMail} success:^(id json) {
                 
                        
                        personInfoModel.infomodel.m.email = newMail;
                        NSData *person = [RFJModel toDataWithModel:personInfoModel];
                        [utils setObject:person forkey:PERSONINFO];

                        [self.navigationController popViewControllerAnimated:YES];
                    } fail:^{}];
                    
                }else{
                    [OMGToast showWithText:@"请输入正确的邮箱。" duration:1.0];
                }
                
            }else{
                [OMGToast showWithText:@"请输入邮箱。" duration:1.0];
            }
        }
}

@end
